package com.we.flink.userlabelplatform.tag;

import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.RichFlatMapFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.util.Collector;

import com.alibaba.fastjson.JSONObject;
import com.we.flink.userlabelplatform.table.TableJiangfeiCardRiskTag;

public class TagJiangfeiCardRiskTag {
    public long uid;
    public int update_time;
    public int jiangfei_card_tag_status;
    public int jiangfei_card_output_time;
    public int jiangfei_card_callback_time;
    public int jiangfei_card_expired_time;

    public TagJiangfeiCardRiskTag() {}

    public TagJiangfeiCardRiskTag(
            long uid,
            int update_time,
            int jiangfei_card_tag_status,
            int jiangfei_card_output_time,
            int jiangfei_card_callback_time,
            int jiangfei_card_expired_time) {
        this.uid = uid;
        this.update_time = update_time;
        this.jiangfei_card_tag_status = jiangfei_card_tag_status;
        this.jiangfei_card_output_time = jiangfei_card_output_time;
        this.jiangfei_card_callback_time = jiangfei_card_callback_time;
        this.jiangfei_card_expired_time = jiangfei_card_expired_time;
    }

    public static TagJiangfeiCardRiskTag init(long uid) {
        return new TagJiangfeiCardRiskTag(uid, 0, -1, 0, 0, 0);
    }

    public static SingleOutputStreamOperator<String> getFlatMapStream(
            StreamTableEnvironment tableEnv, StreamExecutionEnvironment env) {
        Table tableRes = tableEnv.sqlQuery(TableJiangfeiCardRiskTag.QUERYSQL);
        DataStream<Tuple2<Boolean, TableJiangfeiCardRiskTag>> inputStream =
                tableEnv.toRetractStream(tableRes, TableJiangfeiCardRiskTag.class);

        SingleOutputStreamOperator<String> out =
                inputStream
                        .filter(
                                new FilterFunction<Tuple2<Boolean, TableJiangfeiCardRiskTag>>() {
                                    @Override
                                    public boolean filter(
                                            Tuple2<Boolean, TableJiangfeiCardRiskTag> value)
                                            throws Exception {
                                        return value.f0;
                                    }
                                })
                        .uid("TagJiangfeiCardRiskTag-filter")
                        .keyBy(
                                new KeySelector<Tuple2<Boolean, TableJiangfeiCardRiskTag>, Long>() {
                                    @Override
                                    public Long getKey(
                                            Tuple2<Boolean, TableJiangfeiCardRiskTag> value)
                                            throws Exception {
                                        return value.f1.getUid();
                                    }
                                })
                        .flatMap(
                                new RichFlatMapFunction<
                                        Tuple2<Boolean, TableJiangfeiCardRiskTag>, String>() {
                                    ValueState<TagJiangfeiCardRiskTag> jiangfeiCardRiskTagVState;

                                    @Override
                                    public void open(Configuration parameters) throws Exception {
                                        jiangfeiCardRiskTagVState =
                                                getRuntimeContext()
                                                        .getState(
                                                                new ValueStateDescriptor<
                                                                        TagJiangfeiCardRiskTag>(
                                                                        "jiangfeiCardRiskTagVState",
                                                                        TagJiangfeiCardRiskTag
                                                                                .class));
                                    }

                                    @Override
                                    public void flatMap(
                                            Tuple2<Boolean, TableJiangfeiCardRiskTag> value,
                                            Collector<String> out)
                                            throws Exception {
                                        long uid = value.f1.getUid();
                                        int update_time =
                                                (int) (value.f1.getUpdate_time().getTime() / 1000);
                                        int status = value.f1.getStatus();
                                        int output_time =
                                                (int) (value.f1.getOutput_time().getTime() / 1000);
                                        int callback_time =
                                                (int)
                                                        (value.f1.getCallback_time().getTime()
                                                                / 1000);
                                        int expire_time =
                                                (int) (value.f1.getExpire_time().getTime() / 1000);

                                        TagJiangfeiCardRiskTag tmpState =
                                                jiangfeiCardRiskTagVState.value();
                                        TagJiangfeiCardRiskTag curState =
                                                tmpState == null
                                                        ? TagJiangfeiCardRiskTag.init(uid)
                                                        : tmpState;

                                        if (update_time > curState.getUpdate_time()) {
                                            if (curState.getJiangfei_card_tag_status() != status
                                                    || curState.getJiangfei_card_output_time()
                                                            != output_time
                                                    || curState.getJiangfei_card_callback_time()
                                                            != callback_time
                                                    || curState.getJiangfei_card_expired_time()
                                                            != expire_time) {
                                                curState.update_time = update_time;
                                                curState.jiangfei_card_tag_status = status;
                                                curState.jiangfei_card_output_time = output_time;
                                                curState.jiangfei_card_callback_time =
                                                        callback_time;
                                                curState.jiangfei_card_expired_time = expire_time;

                                                JSONObject jsonObject = new JSONObject();
                                                jsonObject.put("uid", uid);
                                                jsonObject.put("jiangfei_card_tag_status", status);
                                                jsonObject.put(
                                                        "jiangfei_card_output_time", output_time);
                                                jsonObject.put(
                                                        "jiangfei_card_callback_time",
                                                        callback_time);
                                                jsonObject.put(
                                                        "jiangfei_card_expired_time", expire_time);

                                                out.collect(jsonObject.toString());
                                                jiangfeiCardRiskTagVState.update(curState);
                                            }
                                        }
                                    }
                                });
        return out;
    }

    public long getUid() {
        return uid;
    }

    public int getUpdate_time() {
        return update_time;
    }

    public int getJiangfei_card_tag_status() {
        return jiangfei_card_tag_status;
    }

    public int getJiangfei_card_output_time() {
        return jiangfei_card_output_time;
    }

    public int getJiangfei_card_callback_time() {
        return jiangfei_card_callback_time;
    }

    public int getJiangfei_card_expired_time() {
        return jiangfei_card_expired_time;
    }

    @Override
    public String toString() {
        return "TagJiangfeiCardRiskTag{"
                + "uid="
                + uid
                + ", update_time="
                + update_time
                + ", jiangfei_card_tag_status="
                + jiangfei_card_tag_status
                + ", jiangfei_card_output_time="
                + jiangfei_card_output_time
                + ", jiangfei_card_callback_time="
                + jiangfei_card_callback_time
                + ", jiangfei_card_expired_time="
                + jiangfei_card_expired_time
                + '}';
    }
}
